home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-02-14 | 18.0 KB | 611 lines | [TEXT/KAHL] |
- /************************************************************************
- * *
- * Utilities.c *
- * *
- * Written by Casper Boon, August, 1992. *
- * *
- * These are a collection of routines taken from my library providing *
- * useful and commonly used functions. *
- * *
- * © 1992 Casper Boon. *
- * *
- ************************************************************************/
-
- void SetExtnsFolder(void);
- void SetPrefsFolder(void);
- void SetSystemFolder(void);
-
- /************************************************************************
- * *
- ************************************************************************/
-
- /* dialutils.c */
-
- /************************************************************************
- * *
- * This is a dialog user item routine installed as the user item *
- * surrounding the default button in a dialog. *
- * *
- ************************************************************************/
- pascal void DefaultFrame(DialogPtr dial, integer item)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- PenSize(2, 2);
- FrameRoundRect(&rect, 16, 16);
- PenNormal();
- }
-
- /************************************************************************
- * *
- * Install a user item routine the easy way. *
- * *
- ************************************************************************/
- void InstallUserItem(DialogPtr dial, integer item, ProcPtr proc)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- type &= ~itemDisable;
- if (type == userItem)
- SetDItem(dial, item, type, (Handle)proc, &rect);
- }
-
- /************************************************************************
- * *
- * Extract the text from a dialog text item the easy way. *
- * *
- ************************************************************************/
- void GetDialText(DialogPtr dial, integer item, StringPtr text)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- type &= ~itemDisable;
- if (type == statText || type == editText)
- GetIText(hand, text);
- else
- text[0] = 0;
- }
-
- /************************************************************************
- * *
- * Set the text of a text item in a dialog *
- * *
- ************************************************************************/
- void SetDialText(DialogPtr dial, integer item, StringPtr text)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- type &= ~itemDisable;
- if (type == statText || type == editText)
- SetIText(hand, text);
- }
-
-
- /************************************************************************
- * *
- * Set the value of a dialog control. *
- * *
- ************************************************************************/
- void SetDialCtlVal(DialogPtr dial, integer item, integer value)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- if (type & ctrlItem)
- SetCtlValue((ControlHandle)hand, value);
- }
-
-
- /************************************************************************
- * *
- * Get the value of a dialog control item *
- * *
- ************************************************************************/
- integer GetDialCtlVal(DialogPtr dial, integer item)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- if (type & ctrlItem)
- return GetCtlValue((ControlHandle)hand);
- return 0;
- }
-
-
- /************************************************************************
- * *
- * Usually used for check boxes or radio buttons, turn the control on *
- * when off or vice versa *
- * *
- ************************************************************************/
- void ToggleDialCtlVal(DialogPtr dial, integer item)
- {
- Handle hand;
- integer type;
- Rect rect;
-
- GetDItem(dial, item, &type, &hand, &rect);
- if (type & ctrlItem)
- SetCtlValue((ControlHandle)hand, (GetCtlValue((ControlHandle)hand))?0:1);
- }
-
- /************************************************************************
- * *
- ************************************************************************/
- /* fileutils.c */
-
- /************************************************************************
- * *
- * Display an error message when something goes wrong in the file *
- * utilities. *
- * *
- ************************************************************************/
- void FileError(StringPtr s, integer e)
- {
- Str255 str;
-
- psprintf(str, "File System Error\015\015%p\015errNo %d", s, e);
- ParamText(str, NIL, NIL, NIL); /* tell dialog manager */
-
- InternalAlert();
- }
-
-
- /************************************************************************
- * *
- * Set the default working directory. Also sets the current directory *
- * for the standard file package. (See tech note 80.) *
- * *
- ************************************************************************/
- void SetWDir(integer wdRef, LongInt wdID, integer vol)
- {
- SetVol(NIL, wdRef); /* wd must be open */
- CurDirStore = wdID; /* will be ignored by MFS */
- SFSaveDisk = vol;
- }
-
-
- /************************************************************************
- * *
- * The complement to SetWDir above. *
- * *
- ************************************************************************/
- void GetWDir(integer *wdRef, LongInt *wdID, integer *vol)
- {
- *wdID = CurDirStore;
- *vol = SFSaveDisk;
- GetVol(NIL, wdRef);
- }
-
-
- /************************************************************************
- ************************************************************************/
- #include <Traps.h>
- #include <Folders.h>
- #include <GestaltEqu.h>
-
- /************************************************************************
- * *
- * Get the blessed folder information *
- * *
- ************************************************************************/
- void GetSystemFolder(integer *vRefNumP, LongInt *dirIDP, integer *sysVRefNum);
- void GetSystemFolder(integer *vRefNum, LongInt *dirID, integer *sysVRefNum)
- {
- SysEnvRec info;
- LongInt wdProcID;
-
- SysEnvirons(1, &info);
- *sysVRefNum = info.sysVRefNum;
- if (GetWDInfo(info.sysVRefNum, vRefNum, dirID, &wdProcID) != noErr)
- {
- log_printf("Failed to get system folder\n");
- *vRefNum = 0;
- *dirID = 0;
- }
- }
-
- Boolean TrapAvailable(LongWord trap);
-
- /************************************************************************
- * *
- * Set the current working directory to the Extensions folder. *
- * *
- ************************************************************************/
- void SetExtnsFolder()
- {
- LongInt feature;
- integer vRefNum, sysVRefNum, err;
- LongInt dirID;
- WDPBRec wdpb;
-
- GetSystemFolder(&vRefNum, &dirID, &sysVRefNum);
-
- if (TrapAvailable(_GestaltDispatch) &&
- Gestalt(gestaltFindFolderAttr, &feature) == noErr)
- {
- if (FindFolder(kOnSystemDisk, kExtensionFolderType,
- kDontCreateFolder, &vRefNum, &dirID) != noErr)
- {
- }
- }
-
- ClearBytes((Ptr)(&wdpb), sizeof(wdpb));
- wdpb.ioNamePtr = NIL;
- wdpb.ioVRefNum = vRefNum;
- wdpb.ioWDDirID = dirID;
- if ( (err = PBHSetVol((HParmBlkPtr)&wdpb, FALSE)) != noErr )
- FileError("\pHSetVol Error", err);
- }
-
- /************************************************************************
- * *
- * Set the current working directory to the Preferences folder. *
- * *
- ************************************************************************/
- void SetPrefsFolder()
- {
- LongInt feature;
- integer vRefNum, sysVRefNum, err;
- LongInt dirID;
- WDPBRec wdpb;
-
- GetSystemFolder(&vRefNum, &dirID, &sysVRefNum);
-
- if (TrapAvailable(_GestaltDispatch) &&
- Gestalt(gestaltFindFolderAttr, &feature) == noErr)
- {
- if (FindFolder(kOnSystemDisk, kPreferencesFolderType,
- kDontCreateFolder, &vRefNum, &dirID) != noErr)
- {
- }
- }
-
- ClearBytes((Ptr)(&wdpb), sizeof(wdpb));
- wdpb.ioNamePtr = NIL;
- wdpb.ioVRefNum = vRefNum;
- wdpb.ioWDDirID = dirID;
- if ( (err = PBHSetVol((HParmBlkPtr)&wdpb, FALSE)) != noErr )
- FileError("\pHSetVol Error", err);
- }
-
- /************************************************************************
- * *
- * Sets the default working directory to be the system folder. (See *
- * Tech notes 66, 67 and 77.) If the name passed is the name of a *
- * folder this will open the folder and set the default directory to *
- * this folder and return TRUE. otherwise we return FALSE. *
- * *
- * Unlike the SetExtnsFolder and SetPrefsFolder above this routine also *
- * sets the standard file valiables to open here. *
- * *
- ************************************************************************/
- integer SetToBlessedFolder(StringPtr aFoldName)
- {
- HVolumeParam myHPB;
- HFileParam myHFPB;
- WDPBRec myWDPB;
- CInfoPBRec myCIPB;
- integer vRef, wdRef,
- retVal = FALSE, osVal;
- LongInt wdID;
-
- ClearBytes((Ptr)(&myHPB), sizeof(myHPB));
- ClearBytes((Ptr)(&myHFPB), sizeof(myHFPB));
- ClearBytes((Ptr)(&myWDPB), sizeof(myWDPB));
- ClearBytes((Ptr)(&myCIPB), sizeof(myCIPB));
-
- myHPB.ioNamePtr = NIL;
- myHPB.ioVRefNum = BootDrive;
- myHPB.ioVolIndex = 0;
-
- if (FSFCBLen > 0)
- {
- GetVRefNum(SysMap, &vRef); /* which volume is system on */
- myHPB.ioVRefNum = vRef;
-
- if (osVal = PBHGetVInfo((HParmBlkPtr)&myHPB, FALSE) )
- FileError("\pPBHGetVInfo", osVal);
- wdID = myHPB.ioVFndrInfo[0]; /* the blessed folder ID */
-
- myWDPB.ioNamePtr = NIL;
- myWDPB.ioVRefNum = vRef;
- myWDPB.ioWDProcID = 'ERIK'; /* created by the system */
- myWDPB.ioWDDirID = wdID;
- if ( osVal = PBOpenWD(&myWDPB, FALSE) ) /* open the blessed folder */
- FileError("\pOpenBlssd", osVal);
-
- /* this is then the refNum of the blessed folder */
- wdRef = myWDPB.ioVRefNum;
-
- if (aFoldName) /* were we given a name? */
- {
- myCIPB.dirInfo.ioNamePtr = aFoldName;
- myCIPB.dirInfo.ioVRefNum = wdRef;
- myCIPB.dirInfo.ioFDirIndex = 0;
- myCIPB.dirInfo.ioDrDirID = 0;
-
- /* get info about object with this name */
- if ( osVal = PBGetCatInfo(&myCIPB, FALSE) )
- {
- myHFPB.ioNamePtr = aFoldName;
- myHFPB.ioVRefNum = vRef;
- myHFPB.ioDirID = wdID;
-
- if ( osVal = PBDirCreate((HParmBlkPtr)&myHFPB, FALSE) )
- {
- FileError("\pPBDirCreate", osVal);
- return FALSE;
- }
- if ( PBGetCatInfo(&myCIPB, FALSE) )
- {
- FileError("\pPBGetCatInfo", osVal);
- retVal = FALSE; /* no such name, creation must have failed */
- }
- }
- if (myCIPB.dirInfo.ioFlAttrib & 0x10)
- { /* it's a directory */
- PBClose((ParmBlkPtr)&myWDPB, FALSE); /* close blessed folder */
-
- wdID = myCIPB.dirInfo.ioDrDirID;
- myWDPB.ioNamePtr = NIL;
- myWDPB.ioVRefNum = vRef;
- myWDPB.ioWDProcID = 'ERIK'; /* */
- myWDPB.ioWDDirID = wdID;
- if ( osVal = PBOpenWD(&myWDPB, FALSE) )
- FileError("\pPBOpenWD", osVal);
-
- wdRef = myWDPB.ioVRefNum;
- retVal = TRUE;
- }
- }
- }
- else PBGetVInfo((ParmBlkPtr)&myHPB, FALSE); /* for "flat" volumes */
-
- SetWDir(wdRef, wdID, -vRef); /* set the default directory */
-
- return retVal;
- }
-
- /************************************************************************
- * *
- * Seems to be missing from the high level file manager, a simple way *
- * to flush the file to disk. This actually only flushes to the disc *
- * buffer and to ensure data integrity the volume must also be flushed *
- * *
- ************************************************************************/
- integer FSFlush(integer fRef)
- {
- HFileParam myHFPB;
-
- ClearBytes((Ptr)(&myHFPB), sizeof(myHFPB));
-
- myHFPB.ioFRefNum = fRef;
- return PBFlushFile((ParmBlkPtr)&myHFPB, FALSE);
- }
-
- /************************************************************************
- * *
- * When reading files we often need only open it to read but FSOpen *
- * tries to open with write permission too and that may fail if the *
- * file has already been opened. FSOpenRO opens the file as read only. *
- * *
- ************************************************************************/
- integer FSOpenRO(StringPtr name, integer vRef, integer *file)
- {
- HFileParam myHFPB;
- integer err;
-
- ClearBytes((Ptr)(&myHFPB), sizeof(myHFPB));
-
- myHFPB.ioNamePtr = name;
- myHFPB.ioVRefNum = vRef;
- myHFPB.filler1 = fsRdPerm; /* actually the ioPermssn byte */
- if ( !(err = PBOpen((ParmBlkPtr)&myHFPB, FALSE) ) )
- *file = myHFPB.ioFRefNum;
- return err;
- }
-
- /************************************************************************
- * *
- * Given a file that is already open for which we only have the name *
- * and volume reference FSGetFRef returns the file reference. This is *
- * used by the spooler when trying to remove a file. If the delete *
- * fails the file is probably open so we find the reference number and *
- * close the file then try deleting it. *
- * *
- ************************************************************************/
- integer FSGetFRef(StringPtr name, integer vRef, integer *fRef)
- {
- HFileParam myHFPB;
- OSErr err;
-
- ClearBytes((Ptr)(&myHFPB), sizeof(myHFPB));
-
- myHFPB.ioNamePtr = name;
- myHFPB.ioVRefNum = vRef;
- err = PBGetFInfo((ParmBlkPtr)&myHFPB, FALSE);
- *fRef = myHFPB.ioFRefNum;
- return err;
- }
-
-
- /************************************************************************
- * *
- ************************************************************************/
- /* utils.c */
-
- /************************************************************************
- * *
- ************************************************************************/
- void ClearBytes(Ptr aPtr, LongInt nBytes)
- {
- asm {
- MOVE.L aPtr, A0
- MOVE.L nBytes, D1
- SUBQ #1, D1
- @0:
- CLR.B (A0)+
- DBRA D1, @0
- }
- }
-
-
- /************************************************************************
- * *
- * Given 2 rectangles in global coords, interpolate one into the other, *
- * making a zooming rectangle image on the screen. The rectangles and *
- * the screen image are not altered. *
- * *
- ************************************************************************/
- void ZoomRect(Rect *smallRect, Rect *bigRect, integer zoomUp)
- {
- #define Fixed LongInt
- #define zoomSteps 16
- #define one 0x10000
-
- Rect rect1, rect2, rect3, rect4;
- integer i, j;
- Fixed factor, fract,
- bt, bl, bb, br,
- st, sl, sb, sr;
- GrafPtr tmpPort;
- GrafPort myPort;
-
- GetPort(&tmpPort);
- OpenPort(&myPort); /* This used to be SetPort(WMgrPort) */
- CopyRgn(GrayRgn, myPort.visRgn); /* but has been changed as per TN194 */
- myPort.portRect = (*GrayRgn)->rgnBBox;
-
- PenMode(notPatXor); /* This is a local pen for myPort */
- PenSize(1,1); /* No need to restore it later */
- PenPat(gray);
-
- if (zoomUp)
- {
- rect1 = *smallRect;
- factor = FixRatio(6, 5); /* make bigger each time */
- fract = FixRatio(541, 10000); /* 5/6 ^16 = 0.540877 */
- }
- else
- {
- rect1 = *bigRect;
- factor = FixRatio(5, 6); /* make smaller each time */
- fract = factor; /* start full size */
- }
-
- rect2 = rect1;
- rect3 = rect1;
-
- sl = smallRect->left; sl = sl << 16; /* scale up to fixed point */
- sr = smallRect->right; sr = sr << 16;
- st = smallRect->top; st = st << 16;
- sb = smallRect->bottom; sb = sb << 16;
-
- bl = bigRect->left; bl = bl << 16; /* scale up to fixed point */
- br = bigRect->right; br = br << 16;
- bt = bigRect->top; bt = bt << 16;
- bb = bigRect->bottom; bb = bb << 16;
-
- FrameRect(&rect1); /* draw initial image */
-
- for (i = 0; i < zoomSteps; i++)
- {
- rect4.left = FixRound(FixMul(fract, bl) + FixMul(one-fract, sl));
- rect4.right = FixRound(FixMul(fract, br) + FixMul(one-fract, sr));
- rect4.top = FixRound(FixMul(fract, bt) + FixMul(one-fract, st));
- rect4.bottom = FixRound(FixMul(fract, bb) + FixMul(one-fract, sb));
-
- FrameRect(&rect4); /* draw newest */
- for (j = 0; j < 25; j++)
- ;
- FrameRect(&rect1); /* erase oldest */
- rect1 = rect2;
- rect2 = rect3;
- rect3 = rect4;
- fract = FixMul(fract, factor); /* bump interpolation fraction */
- }
-
- FrameRect(&rect1); /* erase final image */
- FrameRect(&rect2);
- FrameRect(&rect3);
-
- ClosePort(&myPort);
- SetPort(tmpPort);
- }
-
-
- /************************************************************************
- * *
- * InternalAlert. Several routines require error messages to be *
- * displayed but since they were a part of a library there was no way *
- * of ensuring the dialog existed in the resource file. This uses no *
- * resource. *
- * *
- ************************************************************************/
- static Word itms[] = {
- 3, /* number of items */
-
- 0x0000, 0x0000, /* place holder */
- 80, 208, 100, 285, /* item rectangle */
- 0x0409, /* item type, item length (ctrl+btn, 9) */
- 0x4F68, 0x2046, 0x7269, 0x747A, 0x2100,
- /* item data ( "Oh Fritz!" ) */
-
- 0x0000, 0x0000, /* place holder */
- 77, 205, 103, 288, /* item rectangle */
- 0x8000, /* item type, item length (disabled user item, 0) */
-
- 0x0000, 0x0000, /* place holder */
- 7, 84, 67, 286, /* item rectangle */
- 0x8802, /* item type, item length (disabled static text, 2) */
- 0x5E30, /* item data ( "^0" ) */
-
- 0x0000, 0x0000, /* place holder */
- 8, 16, 40, 48, /* item rectangle */
- 0xA002, /* item type, item length (disabled icon, 2) */
- 0x0002 /* item data ( caution icon ) */
- };
-
-
- /************************************************************************/
- void InternalAlert()
- {
- DialogPtr dial;
- Rect rect;
- Handle Items = NewHandle(sizeof(itms));
- integer itmHit = 0;
-
- HLock(Items);
- BlockMove(itms, *Items, sizeof(itms));
- HUnlock(Items);
-
- SetRect(&rect, 40, 40, 342, 150);
- dial = NewDialog(NIL, &rect, "\p",
- TRUE, dBoxProc, (WindowPtr)-1L, FALSE, 0L, Items);
- SetPort(dial);
-
- InstallUserItem(dial, 2, (ProcPtr)DefaultFrame);
-
- while (itmHit != 1)
- ModalDialog(NIL, &itmHit); /* display the message */
-
- DisposeDialog(dial);
- }
-